package com.inspur.attendance.Controller;

import com.inspur.attendance.Dao.UserDao;
import com.inspur.attendance.Entity.Department;
import com.inspur.attendance.Service.DepartmentService;
import com.inspur.attendance.Tools.Operation;
import com.inspur.attendance.Tools.Return;
import com.inspur.attendance.Tools.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    UserDao userInfoDao;

    @RequestMapping("/getAllDepartment")
    public Return getAllDepartment(@Param("pageNo") Integer pageNo,
                                   @Param("pageSize") Integer pageSize) {
        Page<Department> page1 = departmentService.getAllDepartment(pageNo, pageSize);
        return Return.success(departmentService.getAllDepartment(pageNo, pageSize));
    }

    @Operation
    @RequestMapping("/addDepartment")
    public Return addDepartment(@RequestParam("departmentName") String departmentName,
                                @RequestParam("manager") String manager) {
        System.out.println(departmentName + " " + manager);
        Integer totalUser = 0;
        Department department = new Department();
        String time = TimeConvert.dateToString(new Date());
        System.out.println(time);
        department.setCreateTime(time);
        department.setDepartmentName(departmentName);
        department.setTotalUser(totalUser);
        department.setManager(manager);
        System.out.println(department);
        departmentService.addDepartment(department);
        return Return.success();
    }

    @RequestMapping("/editDepartment")
    public Return editDepartment(@Param("departmentId") Long departmentId,
                                 @RequestParam("manager") String manager) {
        Department department = departmentService.findById(departmentId);
        department.setManager(manager);
        System.out.println(department);
        departmentService.addDepartment(department);
        return Return.success();
    }

    @RequestMapping("/delDepartment")
    public Return delDepartment(@RequestParam("id") Long departmentId) {
        departmentService.delDepartment(departmentId);
        return Return.success();
    }
}
